<!--
 * @Author: 涵崽
 * @Date: 2023-11-07 09:35:18
 * @email: 1045654@qq.com
 * @gitee: https://gitee.com/han-zai
 * @LastEditors: 涵崽
 * @LastEditTime: 2023-12-20 13:55:35
 * @Description: html2Canvas实现截图demo
    标签里要写 crossorigin="anonymous" 
    html2Canvas 配置useCORS: true,
    否则会造成跨域，导致网络图片截图后变成空白
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>html2Canvas实现截图demo</title>
    <style lang="scss">
      .box {
        width: 200px;
        height: 200px;
        background: orange;
      }
    </style>
  </head>
  <body>
    <button id="btn">截图</button>
    <div id="demo">
      <div>
        <img src="https://picsum.photos/200" crossorigin="anonymous" alt="" />
      </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
    <script>
      //   import html2canvas from "html2canvas";
      let btn = document.getElementById("btn");
      btn.onclick = function () {
        let demo = document.getElementById("demo");
        console.log(demo);
        html2canvas(demo, {
          useCORS: true,
        }).then(canvas => {
          let img = new Image();
          img.src = canvas.toDataURL();
          console.log(img);
          demo.innerHTML = "";
          demo.appendChild(img);
        });
      };
    </script>
  </body>
</html>
